const app = getApp()
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
var xrSdk = require('../../../vendor/hope-sdk/index.js');
const util = require('../../../utils/util.js');
Page({
  data: {
    tabs: [{
      name: "全部"
    }, {
      name: "待提交"
    }, {
      name: "待审核"
    }, {
      name: "已审核"
    }, {
      name: "已完成"
    }
    // , {
    //   name: "已评价"
    // }
    ],
    currentTab: 0,
    pageIndex: 1,
    loadding: false,
    pullUpOn: true,
    noContent: false,
    scrollTop: 0,
    orderList:[],
    first:true
  },
  onLoad: function (options) {
    var that = this;
    this.setData({
      currentTab: options.currentTab || 0
    },()=>{
      that.initList();
    });
  },
  edit: function (e) {
    var that = this;
    var order_id = e.currentTarget.dataset.id;
    var tyre_id = e.currentTarget.dataset.tyre;
    app.WxService.navigateTo("../edit/edit", {
      order_id: order_id,
      tyre_id: tyre_id
    });
  },
  sure:function(e){
    var that = this;
    var order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定完成吗',
      content: '确定新轮胎已经安装完成吗？',
      success(res) {
        if (res.confirm) {
          that.sureHandler(order_id);
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });    
  },
  sureHandler:function(order_id){
    var that = this;
    xrRequest.request(app,{
      url: config.service.sureOrder,
      login: true,
      data: {
        order_id: order_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
          that.onPullDownRefresh();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
       },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  submit:function(e){
    var that = this;
    var order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定提交吗',
      content: '确定提交本次鼓包更换申请吗？',
      success(res) {
        if (res.confirm) {
          that.submitHandler(order_id);
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });    
  },
  submitHandler: function (order_id) {
    var that = this;
    xrRequest.request(app,{
      url: config.service.submitOrder,
      login: true,
      auth: true,
      data: {
        order_id: order_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
          that.onPullDownRefresh();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) { 
        wx.showToast({
          title: res,
          icon: 'none',
        })
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  delete:function(e){
    var that = this;
    var order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定删除吗',
      content: '确定删除本次鼓包更换申请吗？',
      success(res) {
        if (res.confirm) {
          that.deleteHandler(order_id);
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });    
  },
  deleteHandler: function (order_id) {
    var that = this;
    xrRequest.request(app,{
      url: config.service.deleteOrder,
      login: true,
      auth: true,
      data: {
        order_id: order_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
          that.onPullDownRefresh();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
       },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  change(e) {
    this.setData({
      currentTab: e.detail.index
    })
    this.onPullDownRefresh();
  },
  detail:function(e) {
    var order_id = e.currentTarget.dataset.id;
    app.WxService.navigateTo("../view/view", {
      id: order_id
    });
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  initData: function () {
    this.setData({
      orderList: [],
      pageIndex: 1,
      pullUpOn: true,
      loadding: true,
      noContent: false
    })
  },
  initList: function () {
    var that = this;
    var params = that.getParams();
    xrRequest.request(app,{
      url: config.service.orderList,
      login: false,
      data: params,
      success: function (res) {
        that.setData({
          orderList: that.data.orderList.concat(res.data.data.data),
          pageIndex: that.data.pageIndex + 1,
          loadding: false
        })
        // 没有更多
        if (res.data.data.has_more == false &&
          that.data.orderList.length > 0) {
          that.setData({
            pullUpOn: false
          })
        }
        // 空数据
        that.setData({
          noContent: that.data.orderList.length == 0
        });
        wx.stopPullDownRefresh();
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    });
  },
  getParams() {
    var that = this;
    var params = {
      page: that.data.pageIndex,
      page_size: 10,
      status: that.data.currentTab
    };
    return params;
  },
  onPullDownRefresh() {
    this.initData();
    this.initList();
  },
  onReachBottom() {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    }, () => {
      this.initList();
    })
  },
  onClick:function(e){
    app.WxService.navigateTo("../apply/apply");
  },
  onShow:function(){
    if (!this.data.first) {
      this.initData();
      this.initList();
    }
    this.setData({
      first: false
    });
  }
})